package br.com.br1.fv.model;

import java.util.Collection;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

public class GenericDao implements Dao {

	private DataSource ds;
	private String tableName;

	public void create(ValueObject vo) {

		Map<String, Object> fields = vo.getProperties();
		StringBuffer fieldsBuf = new StringBuffer();
		StringBuffer valuesBuf = new StringBuffer();

		String sep = null;
		for(String fieldName:fields.keySet()){
			if(sep != null){
				fieldsBuf.append(sep);
				valuesBuf.append(sep);
			}
			fieldsBuf.append(fieldName);
			valuesBuf.append(":").append(fieldName);
			sep = ",";
		}
		NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(ds);
		jdbc.update(String.format("INSERT INTO %s (%s) VALUES (%s)", tableName, fieldsBuf, valuesBuf), fields);
	}

	public void update(ValueObject vo, QueryCriteria criteria) {
	}

	public void remove(QueryCriteria criteria) {
	}

	public Collection<ValueObject> select(QueryCriteria criteria) {
		return null;
	}

	public Collection<ValueObject> nativeSelect(String query, Map<String, Object> parameters) {
		return null;
	}
	
	public String getTableName() {
		return tableName;
	}
	
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public DataSource getDataSource() {
		return ds;
	}

	public void setDataSource(DataSource ds) {
		this.ds = ds;
	}
}
